package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.NewsDao;
import com.cn.demo4.model.NewsModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements NewsDao {

    @Override
    public List<NewsModel> findAll(NewsModel newsModel) {
        //1.书写sql查询语句
//        String sql="select n.*, t.name type_name from news n,news_type t where n.type_id= t.id order by id desc";
        StringBuilder sql = new StringBuilder();
        sql.append( "select n.*, t.name type_name from news n,news_type t where true and n.type_id= t.id");
        if(!"".equals(newsModel.getTitle())){
            sql.append(" and title like '%").append(newsModel.getTitle()).append("%'");
        }
        if(!"全部".equals(newsModel.getTypeName())){
            sql.append(" and t.name like '%").append(newsModel.getTypeName()).append("%'");
        }
        if(!"".equals(newsModel.getPublisher())){
            sql.append(" and publisher like '%").append(newsModel.getPublisher()).append("%'");
        }
        if(newsModel.getStatus() != -1){
            if(newsModel.getStatus() == 1){
                sql.append(" and n.status=1");
            }else{
                sql.append(" and n.status is null and n.status=0");
            }
        }
        sql.append(" order by n.id desc");
        sql.append(" limit ").append((newsModel.getPageModel().getCurPage() - 1) * newsModel.getPageModel().getPageSize()).append(",").append(newsModel.getPageModel().getPageSize());
        //2.返回查询结果存入ResultSet中
        ResultSet resultSet = JDBC.select(sql.toString());
        return getData(resultSet);
    }

    @Override
    public int add(NewsModel newsModel) {
        String sql = "insert into news(title,content,img,type_id,status,publisher,create_time,update_time) " +
                "values('" + newsModel.getTitle() + "','" + newsModel.getContent() + "','" + newsModel.getImg() + "'," +
                "" + newsModel.getTypeId() + "," +
                "" + newsModel.getStatus() + "," +
//                "" + newsModel.getIsShow() + "," +
                "'" + newsModel.getPublisher() + "'," +
                "now(),now())";
        return JDBC.upDate(sql);
    }

    @Override
    public NewsModel findById(Integer id) {
        String sql = "select n.* ,t.name type_name from news n , news_type t where n.type_id=t.id and n.id=" + id;
        ResultSet resultSet = JDBC.select(sql);
        //        List<ProductModel> list = getData(resultSet);
        //        return list.size() > 0 ? list.get(0) : null;
        return getData(resultSet).get(0);
    }

    @Override
    public int update(NewsModel newsModel) {
        String sql = "update news set title='" + newsModel.getTitle() + "',";
        sql += " content='" + newsModel.getContent() + "',";
        if(newsModel.getImg() !=null && !newsModel.getImg().equals("undefined")){
            sql += " img='" + newsModel.getImg() + "',";
        }
        sql += "type_id='" + newsModel.getTypeId() + "'," +
//                "is_show=" + newsModel.getIsShow() + "," +
                "status=" + newsModel.getStatus() + "," +
                "publisher='" + newsModel.getPublisher() + "'," +
                "update_time=now()" +
                " where id=" + newsModel.getId();
        return JDBC.upDate(sql);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from news where id="+id;
        return JDBC.upDate(sql);
    }

    @Override
    public List<NewsModel> getNewsModelListForHome() {

        //1.书写sql查询语句
        String sql="select n.*, t.name type_name from news n,news_type t where n.type_id= t.id and n.status=1";
        //2.返回查询结果存入ResultSet中
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    @Override
    public int geCount(NewsModel newsModel) {
        StringBuilder sql = new StringBuilder();
        sql.append( "select count(*) count from news n,news_type t where true and n.type_id= t.id");
        if(!"".equals(newsModel.getTitle())){
            sql.append(" and title like '%").append(newsModel.getTitle()).append("%'");
        }
        if (!"全部".equals(newsModel.getTypeName()) && newsModel.getTypeName() != null) {
            sql.append(" and t.name like '%").append(newsModel.getTypeName()).append("%'");
        }
        if(!"".equals(newsModel.getPublisher())){
            sql.append(" and publisher like '%").append(newsModel.getPublisher()).append("%'");
        }
        if(newsModel.getStatus() != -1){
            if(newsModel.getStatus() == 1){
                sql.append(" and n.status=1");
            }else{
                sql.append(" and n.status is null and n.status=0");
            }
        }
        sql.append(" order by n.id desc");
        //2.返回查询结果存入ResultSet中
        ResultSet resultSet = JDBC.select(sql.toString());
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 公共得到数据方法
     *
     * @param resultSet
     * @return
     */
    private List<NewsModel> getData(ResultSet resultSet) {
        List<NewsModel> list = new ArrayList<>();
        try {
            //遍历数据
            while (resultSet.next()) {
                NewsModel newsModel = new NewsModel();
                newsModel.setId(resultSet.getInt("id"));
                newsModel.setTitle(resultSet.getString("title"));
                newsModel.setContent(resultSet.getString("content"));
                newsModel.setImg(resultSet.getString("img"));
                newsModel.setTypeId(resultSet.getInt("type_id"));
                newsModel.setTypeName(resultSet.getString("type_name"));
                newsModel.setIsShow(resultSet.getInt("is_show"));
                newsModel.setStatus(resultSet.getInt("status"));
                newsModel.setPublisher(resultSet.getString("publisher"));
                newsModel.setCreateTime(resultSet.getString("create_time"));
                newsModel.setUpdateTime(resultSet.getString("update_time"));
                list.add(newsModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
